<?php
class Cms_ArticleController extends Zend_Controller_Action {
    private $_languages = array();
    private $_registerCats = array(2,//Hoi Thao 
                                4,//Hoc bong
    );
    public function init() {
        $this->_languages = Utils_Global::getSupportLanguages();
    }
    
	public function detailAction() {
		$id = Utils_Global::$params['id'];
		$lang = Utils_Global::$params['lang'];
		if(!in_array($lang, $this->_languages)) {
			$lang = Utils_Global::getCurrentLanguage();
		}
		$model = Cms_Model_Article::factory();
		$options = array('id' => $id, 'lang' => $lang);
		$article = $model->getDetailArticle($options);
		$article = $article[0];
		$otherArticles = $model->getArticles(array('lang' => $lang, 'category' => $article['category'], 'offset' => 0, 'limit' => 11));
		$breadcrumbData = array(
				array('url' => $this->view->serverUrl(),
						'displayName' => $this->view->translate('home', $lang),
						'title' => $this->view->translate('home', $lang),
				),
				array(
						'url' => 'javascript:void(0)',
						'displayName' => $this->view->translate('article', $lang),
						'title' => $this->view->translate('article', $lang),
				),
				array(
						'url'=> 'javascript:void(0)',
						'displayName' => $article['title'],
						'title' => $article['title'],
				)
		);
		
		if(in_array($article['category'], $this->_registerCats)) {//Catory hoi thao/hoc bong
		    unset($breadcrumbData[1]);
		}
		
		$this->view->breadcrumb = $breadcrumbData;
		$this->view->lang = $lang;
		$this->view->article = $article;
		$this->view->otherArticles = $otherArticles;
		$metaInfo = array('title' => $article['seo_title_content'],
				'description' => $article['seo_description_content'],
		        'keyword' => $article['seo_keyword_content'],
		);
		$this->view->metadata = $this->view->metadata('', $metaInfo,
				$this->view->serverUrl() . $this->view->url(array()));
		
		if(in_array($article['category'], $this->_registerCats)) {//Category hội thảo
		     $this->render('workshopdetail');   
		}
	}
	
	public function articlesAction() {
	    $page = intval(Utils_Global::$params['page']);
	    $limit = intval(Utils_Global::$params['limit']);
	    $category = intval(Utils_Global::$params['category']);
	    $lang = Utils_Global::$params['lang'];
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    
	    if($page < 1) {
	    	$page = 1;
	    }
	    if($limit <= 0){
	    	$limit = 16;
	    }
	    
	    
	    $model = Cms_Model_Article::factory();
	    $options = array('category' => $category, 'lang' => $lang,
	                        'offset' => ($page - 1) * $limit, 'limit' => $limit, 
	                        'order' => 'order', 'by' => 'DESC');
	    $sharedArticles = $model->getArticles($options);
	    $keyTitle = "article";
	    $seokeyTitle = "seo_article_title";
	    $seokeyDescription = "seo_article_description";
	    $keyMeta = "fmit_shareexperience"; 
	    if($category == 3) {
	        $keyTitle = "subnavigation_shareexpericence";
	        $keyMeta = "fmit_shareexperience";
	        $seokeyTitle = "seo_page_tvdn_title";
	        $seokeyDescription="seo_page_tvdn_description";
	    }
	    if($category == 5) {
	        $keyTitle = "subnavigation_ketquathi";
	    }
	    if($category == 4) {
	        $keyTitle = "subnavigation_hocbong";
	    }
	    $breadcrumbData = array(
	    		array('url' => $this->view->serverUrl(),
	    				'displayName' => $this->view->translate('home', $lang),
	    				'title' => $this->view->translate('home', $lang),
	    		),
	    		array(
	    				'url' => 'javascript:void(0)',
	    				'displayName' => $this->view->translate($keyTitle, $lang),
	    				'title' => $this->view->translate($keyTitle, $lang),
	    		),
	    );
	    $this->view->metadata = $this->view->metadata($keyMeta, array('title' => $this->view->translate($seokeyTitle, $lang),
	                                                                   'description' => $this->view->translate($seokeyDescription, $lang),
	    ),
	    $this->view->serverUrl() . $this->view->url(array()));
	    $this->view->breadcrumb = $breadcrumbData;
	    $this->view->lang = $lang;
	    $this->view->bigArticle = array_shift($sharedArticles);
	    $this->view->articles = $sharedArticles;
	    $this->view->page = $page;
	    $this->view->totalItem = $model->getArticleCount($options);
	    $this->view->numRowPerPage = $limit;
	    $this->view->staticUrl =  Utils_Global::get('staticUrl') . '/article/';
	    $this->view->category = $category;
	    Utils_Global::$params['newsclzz'] = 'current';
	    if(in_array($category, array(2,3,4,5))) {//Ket qua thi
	        unset(Utils_Global::$params['newsclzz']);
	    }
	    if(Utils_Global::get('isAjax')) {
	    	$this->_helper->layout()->disableLayout();
	    	$this->render('articles-content');
	    } else {
	        $this->view->content = $this->view->render('article/articles-content.phtml');
	    }
	}
	
	public function registerWorkshopAction() {
	    $fullname = trim(Utils_Global::$params['fullname']);
	    $email = trim(Utils_Global::$params['email']);
	    $phone = trim(Utils_Global::$params['phone']);
	    $attenderCount = Utils_Global::$params['count'];
	    $address = Utils_Global::$params['address'];
	    $company = Utils_Global::$params['company'];
	    $title = Utils_Global::$params['title'];
	    $lang = Utils_Global::$params['lang'];
	    $id = Utils_Global::$params['article_id'];
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    $result = array();
	    if(!$fullname) {
	    	$result['code'] = 0;
	    	$result['message'] = 'Vui lòng nhập họ tên';
	    	$this->_helper->json($result);
	    }
	    if(!$email || !Utils_CommonFunction::isValidEmail($email)) {
	    	$result['code'] = 0;
	    	$result['message'] = 'Vui lòng nhập email hợp lệ';
	    	$this->_helper->json($result);
	    }
	    if(!$phone || !Utils_CommonFunction::isValidPhone($phone)) {
	    	$result['code'] = 0;
	    	$result['message'] = 'Vui lòng nhập số điện thoại hợp lệ';
	    	$this->_helper->json($result);
	    }
	    if(!$attenderCount) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng cho biết số người tham dự';
	        $this->_helper->json($result);
	    }
	    if(!$id) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng chọn hội thảo';
	        $this->_helper->json($result);
	    }
	    //Render email
	    $this->view->fullname = $fullname;
	    $this->view->email = $email;
	    $this->view->phone = $phone;
	    $this->view->address = $address;
	    $this->view->company = $company;
	    $this->view->title = $title;
	    $this->view->count = $attenderCount;
	    $model = Cms_Model_Article::factory();
	    $options = array('id' => $id, 'lang' => $lang);
	    $article = $model->getDetailArticle($options);
	    if($article[0]['category'] == 2) {
	        $subfixtitle = " hội thảo ";
	        $workShop = $model->getWorkshops($options);
	    } else {
	        $subfixtitle = " học bổng ";
	    }
	    $this->view->article = $article[0];
	    $this->view->location = $workShop[0]['location'];
	    $this->view->date = Utils_CommonFunction::getDay($workShop[0]['workshop_date']) . " " . date('d/m/Y H:i', $workShop[0]['workshop_date']);
	    $this->view->subfix = $subfixtitle;
	    $emailContent = $this->view->render('article/registerworkshopmail.phtml');
	    //send email to fmit
	    $fmitEmail = Utils_Global::getConfig('cms', 'application', 'fmithoithao');
	    $fmitEmail = $fmitEmail->toArray();
	    $sent = Utils_CommonFunction::sendMail($fmitEmail, 'Đăng ký tham dự' . $subfixtitle, $emailContent);
	    if($sent) {
	    	$result['code'] = 1;
	    	$result['message'] = $this->view->translate('success_registration', $lang);
	    	$this->_helper->json($result);
	    } else {
	    	$result['code'] = 0;
	    	$result['message'] = "Có lỗi xảy ra, vui lòng thử lại!";
	    	$this->_helper->json($result);
	    }
	}
}
